#coding=utf-8
import os

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from settings import savedriver
import time
'''
    webdriver初始化
    
    set_driver(url) 传入初始url 初始化driver并返回driver
    destroy_driver() case全部执行完关闭driver 
'''



def set_driver(url):

    driver_path = os.path.join(os.path.dirname(__file__), './chromedriver')

    # 创建chrome参数对象
    chrome_options = Options()

    # 把chrome设置成无界面模式，不论windows还是linux都可以，自动适配对应参数
    # chrome_options.set_headless()

    chrome_options.add_argument('--no-sandbox') #解决DevToolsActivePort文件不存在的报错

    chrome_options.add_argument('window-size=1920x1080') #指定浏览器分辨率
    chrome_options.add_argument('--disable-gpu') #谷歌文档提到需要加上这个属性来规避bug
    chrome_options.add_argument('--hide-scrollbars') #隐藏滚动条, 应对一些特殊页面
    chrome_options.add_argument("–incognito")#开启无痕模式
    # chrome_options.add_argument('blink-settings=imagesEnabled=false') #不加载图片, 提升速度
    # chrome_options.add_argument('--headless') #浏览器不提供可视化页面. linux下如果系统不支持可视化不加这条会启动失败

    driver = webdriver.Chrome(executable_path=driver_path,options=chrome_options)

    # driver.implicitly_wait(10)

    driver.get(url)


    return driver


def destroy_driver(driver):

    driver.close()




if __name__ == "__main__":

    driver = set_driver("http://taishan.huimei.com")
    destroy_driver(driver)